package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.ashGate;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CAshGateVideoBind;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICAshGateVideoBindService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AshGateAdvancedBu {
    @Autowired
    private ICAshGateVideoBindService ashGateVideoBindService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private SysLogBu sysLogBu;

    //ManualGateStatus 是否手动料位   ManualGateHighAndManStatus  设置手动料位高度和操作员
    public SzResult ashGateAdvanced(String id, String ManualGateStatus, String ManualGateHigh, String token,String plantId) throws Exception {
        QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("ASHGATE_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getOne(queryWrapper);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        if (ashGateVideoBind == null) {
            return SzResult.Error("该卡号不存在");
        }
        if (ManualGateStatus != null) {
            if (ManualGateStatus.equals("1")) {
                ashGateVideoBind.setAshgateMan(String.valueOf(1));//手动料位
                ashGateVideoBind.setDownedID1(0);
                ashGateVideoBindService.updateById(ashGateVideoBind);
                sysLogBu.logRecord("手动料位", user.getTrueName(), "修改了料位名称为" +  ashGateVideoBind.getAshgateName() + "的料位的手动料位为【是】",plantId);
            } else if (ManualGateStatus.equals("0")) {
                ashGateVideoBind.setAshgateMan(String.valueOf(0));//自动料位
                ashGateVideoBind.setDownedID1(0);
                ashGateVideoBindService.updateById(ashGateVideoBind);
                sysLogBu.logRecord("手动料位", user.getTrueName(), "修改了料位名称为" +  ashGateVideoBind.getAshgateName() + "的料位的手动料位为【否】", plantId);
            }
        }
        if (ManualGateHigh != null&& !ManualGateHigh.isEmpty()) {
            String ashGateMan = ashGateVideoBind.getAshgateMan();
            if (ashGateMan.equals("0")) {
                return SzResult.Error("该料位不是手动料位,不可设置手动料位高度");
            }
            ashGateVideoBind.setAshgateCurHghtMan(ManualGateHigh);
            SysUser user1 = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            ashGateVideoBind.setAshgateCurHghtManOperator(user.getTrueName());
            ashGateVideoBind.setDownedID1(0);
            ashGateVideoBindService.updateById(ashGateVideoBind);
            sysLogBu.logRecord("修改区域手动料位高度管理", user1.getTrueName(), "修改了区域名称为" + ashGateVideoBind.getAshgateName() + "的手动料位高度管理权限", plantId);
        }
        ashGateVideoBind.setDownedID1(0);
        ashGateVideoBindService.updateById(ashGateVideoBind);
        return SzResult.Success();
    }
}
